import time

def bubble_sort(arr):
    for j in range(len(arr)):
        was_swap = False
        for i in range(1, len(arr)):
            if arr[i - 1] > arr[i]:
                arr[i - 1], arr[i] = arr[i], arr[i - 1]
                was_swap = True
        if not was_swap:
            break
    return arr

def convertToIntL(l):
    new_l = []
    for item in l:
        new_l.append(int(item))
    return new_l

def is_sorted(l):
    for i in range(1, len(l)):
        if l[i - 1] > l[i]:
            return False
    return True

def read_list(file_name):
    file = open(file_name, 'r')
    lines = file.readlines()
    return convertToIntL(lines)

files1 = ['m1_sorted.txt']
files2 = ['m2_reverse.txt']
files3 = ['m3_shuffle.txt']

for file in files1:
    lines = read_list(file)
    t_start_sec = time.time()
    sorted_arr = bubble_sort(lines)
    t_finish_sec = time.time()
    if is_sorted(sorted_arr):
        print('m1_sorted', round((t_finish_sec - t_start_sec) * 100), 'milliseconds')
    else:
        print('wrong answer')

for file in files2:
    lines = read_list(file)
    t_start_sec = time.time()
    sorted_arr = bubble_sort(lines)
    t_finish_sec = time.time()
    if is_sorted(sorted_arr):
        print('m2_reverse', round((t_finish_sec - t_start_sec) * 100), 'milliseconds')
    else:
        print('wrong answer')

for file in files3:
    lines = read_list(file)
    t_start_sec = time.time()
    sorted_arr = bubble_sort(lines)
    t_finish_sec = time.time()
    if is_sorted(sorted_arr):
        print('m3_shuffle', round((t_finish_sec - t_start_sec) * 100), 'milliseconds')
    else:
        print('wrong answer')